/**
 * RoomInfoController.java
 * com.game.hall.api
 * Copyright (c) 2016, 北京聚智未来科技有限公司版权所有.
 */

package com.game.hall.api;

import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSON;
import com.eova.common.constant.ConstantValue;
import com.eova.common.id.Sequence;
import com.game.hall.model.GameUser;
import com.game.hall.model.RoomInfo;
import com.game.hall.model.RoomUserInfo;
import com.game.hall.model.WXGameUser;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;

/**
 * 
 * 创建房间的信息
 * 
 * @author gyp
 * @Date 2016年12月9日
 * @Time 下午6:30:57
 */
public class RoomInfoApi extends Controller {

	/**
	 * 描述:获取房间的列表 作者:gyp
	 */
	public void roomListAPI() {

		String obj = getPara("userInfo");

		WXGameUser wgu = JSON.parseObject(obj, WXGameUser.class);
		GameUser gameuser = getModel(GameUser.class);
		boolean flag = GameUser.dao.addGameUser(wgu, gameuser);
		if (flag) {
			List<RoomInfo> rooms = RoomInfo.dao.getRoomInfoList();
			renderJson(rooms);// 获取返回的数据
		} else {
			// TODO 执行添加失败后的操作
			setAttr("result", ConstantValue.EOVA_FAIL);// fail表示失败
			renderJson();// 获取返回的数据
		}
	}

	/**
	 * 创建房间的接口 作者:gyp roomName:房间名称
	 * 
	 */
	public void createRoomAPI() {

		String roomName = getPara("roomName");
		String roomTitle = getPara("roomTitle");
		String roomPwd = getPara("roomPwd");
		String userInfo = getPara("userInfo");

		WXGameUser wgu = JSON.parseObject(userInfo, WXGameUser.class);
		WXGameUser gameuser = (WXGameUser) getSession().getAttribute("wxUser");
		// get Model
		RoomInfo ri = getModel(RoomInfo.class);
		String roomId = Sequence.nextId();
		ri.set("id", roomId);
		ri.set("roomName", roomName);
		ri.set("roomTitle", roomTitle);
		ri.set("roomPwd", roomPwd);
		ri.set("roomPeopleNum", ConstantValue.EOVA_ROOMINFO_NUM);// 房间的人数
		ri.set("roomStatus", ConstantValue.EOVA_ROOMSTATUS_WAIT);// 定义状态
		ri.set("createTime", new Date());// 创建时间
		ri.set("isDelete", ConstantValue.EOVA_DELETE_NO);// 是否删除的状态
		boolean flag = ri.save();
		if (flag) {
			// 房间创建成功，游戏的用户表中添加信息
			GameUser gu = GameUser.dao.getGameUserByNickName(wgu.getNickName());
			if (null != gu) {
				RoomUserInfo rui = getModel(RoomUserInfo.class);
				String roomUserInfoId = Sequence.nextId();// 生成的id
				rui.set("id", roomUserInfoId);
				rui.set("userId", gu.get("id"));
				rui.set("roomId", roomId);
				rui.set("isReady", ConstantValue.EOVA_YES);
				rui.set("isHomeowners", ConstantValue.EOVA_YES);
				rui.set("addRoomTime", new Date());
				boolean f = rui.save();
				if (f) {
					setAttr("id", roomId);//
					setAttr("result", ConstantValue.EOVA_SUCCESS);// success表示成功
					renderJson();
				} else {
					// TODO 执行失败之后的操作
					setAttr("result", ConstantValue.EOVA_FAIL);// fail表示失败
					renderJson();
				}
			}
			// renderJson("{'result':'success'}");//自己定义json
		} else {
			// TODO 执行失败之后的操作
			setAttr("result", ConstantValue.EOVA_FAIL);// fail表示失败
			renderJson();
		}
	}

	/**
	 * 
	 * 描述: 加入房间 作者:gyp
	 */
	public void addRoomInfoAPI() {

		String userInfo = getPara("userInfo");
		WXGameUser wgu = JSON.parseObject(userInfo, WXGameUser.class);
		GameUser gu = GameUser.dao.getGameUserByNickName(wgu.getNickName());
		String roomId = getPara("roomId");// 获取房间的id
		RoomUserInfo rui = getModel(RoomUserInfo.class);
		boolean flag = RoomUserInfo.dao.saveRoomUserInfo(rui, gu.get("id")
				.toString(), roomId);
		if (flag) {
			setAttr("result", ConstantValue.EOVA_SUCCESS);// success表示成功
			renderJson();
		} else {
			// TODO 执行失败之后的操作
			setAttr("result", ConstantValue.EOVA_FAIL);// fail表示失败
			renderJson();
		}
	}

	/**
	 * 
	 * 描述: 退出房间 退出房间从表中把该数据删除根据的是用户的id 作者:gyp
	 */
	public void quitRoom() {
		String userId = getPara("userId");// 获取参于用户的userId
		String infoid = getPara("infoid");// 获取参于用户的userId

		String delsqlUser = "DELETE FROM room_user_info WHERE userId=?";
		String delsqlInfo = "DELETE FROM game_detail_info WHERE id=?";
		int iRet = Db.update(delsqlUser, userId);// return 1 success
		int aa = Db.update(delsqlInfo, infoid);// return 1 success
		if (iRet == 1 && aa == 1) {
			RoomUserInfo rui = RoomUserInfo.dao.getInfoByUserId(userId);// 获取要推送人员的信息
			setAttr("flag", ConstantValue.EOVA_TRUE);// success表示成功
			renderJson();
		} else {
			setAttr("flag", ConstantValue.EOVA_FALSE);// fail表示失败
			renderJson();
		}
	}

	public void dissolvedRoom() {

		String roomId = getPara("roomId");// 获取参于用户的userId
		String delsql = "DELETE FROM room_user_info WHERE roomId=?";
		int iRet = Db.update(delsql, roomId);// return 1 success

		if (iRet == 1) {

			setAttr("result", ConstantValue.EOVA_TRUE);// success表示成功
			renderJson();
		} else {
			setAttr("result", ConstantValue.EOVA_FALSE);// fail表示失败
			renderJson();
		}

		// String userId = getPara("userId");// 获取参于用户的userId
		// RoomUserInfo rui = RoomUserInfo.dao.getInfoByUserId(userId);

	}

}
